#include "templatecall.h"
#include "template.h"
#include "parameterlist.h"
#include "errorlog.h"
#include "membertemplate.h"

TemplateCall::TemplateCall(const QString& name, ExpressionList* args, const YYLTYPE& loc)
        : Node(loc), m_name( name ), m_args( args )
{
    appendChild( args );
}


void TemplateCall::resolveAll()
{
    this->Node::resolveAll();

    m_resolvedNode = this->nspace()->resolve(this->m_name);
    if ( !m_resolvedNode )
    {
        ErrorLog::error( location(), "Unknown template " + m_name );
        return;
    }

    if ( resolvedTemplate() )
    {
        // Does the number of parameters match?
        if ( resolvedTemplate()->parameterList()->count() != this->m_args->count() )
            ErrorLog::error( location(), "The template " + m_name + " requires a different number of arguments" );
    }
    else if ( resolvedMemberTemplate() )
    {
        // Does the number of parameters match?
        if ( resolvedMemberTemplate()->parameterList()->count() != this->m_args->count() )
            ErrorLog::error( location(), "The template " + m_name + " requires a different number of arguments" );
    }
    else
    {
        ErrorLog::error( location(), m_name + " is not a template" );
        return;
    }
}

Template* TemplateCall::resolvedTemplate() const
{
    return dynamic_cast<Template*>(m_resolvedNode);
}

MemberTemplate* TemplateCall::resolvedMemberTemplate() const
{
    return dynamic_cast<MemberTemplate*>(m_resolvedNode);
}

